
#define MAXQ 20
#define TAM 128

using namespace std;


int socket_creacion()
{

	return (socket(AF_INET,SOCK_STREAM,0));

}


void socket_conectar_servidorST(int socket_fd)
{
        int error;


         char ip_servidor[128]="grep ip ../config/cliente.ini | cut -d'=' -f2";
         char puerto[128]="grep port ../config/cliente.ini | cut -d'=' -f2";

         FILE *ip_file = popen(ip_servidor,"r");
         char ip_buffer[100];
         fscanf(ip_file,"%100s",ip_buffer);
         pclose(ip_file);

         FILE *puerto_file = popen(puerto,"r");
         char puerto_buffer[128];
         fscanf(puerto_file,"%100s",puerto_buffer);
         pclose(puerto_file);


        struct sockaddr_in listen_address;


        listen_address.sin_family = AF_INET; // Llenamos la estructura
        listen_address.sin_port = htons(5656); // con la info del
        listen_address.sin_addr.s_addr = inet_addr("192.168.1.150"); // servidor.
        error = connect(socket_fd, (struct sockaddr *)&listen_address, sizeof(struct sockaddr)); // Conectamos el socket

        if(error == -1)
        {
             cout<<"Error connect"<<endl;
             exit(1);
        }
}



int crear_socket_SP()
{

        int listen_socket_SP;

        unsigned short int listen_port = 0;
        unsigned long int listen_ip_address = 0;
        struct sockaddr_in listen_address, con_address;
        socklen_t con_addr_len;

       // obtengo el puerto de cliente.ini
         char puerto[128]="grep port ../config/cliente.ini | cut -d'=' -f2";
         FILE *puerto_file = popen(puerto,"r");
         char puerto_buffer[128];
         fscanf(puerto_file,"%100s",puerto_buffer);
         pclose(puerto_file);

        //Creamos el Socket de Escucha
        listen_socket_SP = socket(AF_INET, SOCK_STREAM, 0);

        //Asigno direccion y configuracion.puerto_escucha
        bzero(&listen_address, sizeof(listen_address));
        listen_address.sin_family = AF_INET;
        listen_port = htons(atoi("5656"));
        listen_address.sin_port = listen_port;
        listen_address.sin_addr.s_addr = htonl(INADDR_ANY);
       

        if((bind(listen_socket_SP,(struct sockaddr*)&(listen_address), sizeof(struct sockaddr)))<0){
                fprintf(stderr , "Error al asignar la Direccion IP.\n");
                exit(0);
        }

        //Comenzamos a escuchar conexiones
        if( (listen(listen_socket_SP,MAXQ)) < 0 ){
                fprintf(stderr , "Error al escuchar conexiones.\n");
                exit(0);
        }

        return listen_socket_SP;

}


